{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "86ce47db-a416-4f54-8eed-70244afb504f",
   "metadata": {},
   "source": [
    "# 采用兼容的OpenAI方式访问3Stooges API"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6318bbf8-ecfb-44f6-9732-562171a33dcf",
   "metadata": {},
   "source": [
    "## 方法1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6a763e1f-52a4-4ba8-adb8-432e483a2882",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "# from params import OpenAI_MODEL, OPENAI_API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "34581868-96ae-4f38-beb7-f9ce2d2c6f76",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OpenAI(api_key=\"sk-1234\", base_url=\"http://1.15.125.13:3033/v1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "213c46ca-1e7d-4c7e-b7d4-aaf6a1c56f48",
   "metadata": {},
   "outputs": [],
   "source": [
    "msg_context = {\"role\": \"system\", \"content\": \"hi\"}\n",
    "\n",
    "msg_list = [\n",
    "    msg_context, \n",
    "    {\n",
    "        \"role\": \"user\", \n",
    "        \"content\": f\"What is digital twin?\"\n",
    "    }\n",
    "]\n",
    "\n",
    "\n",
    "\n",
    "completion = client.chat.completions.create(\n",
    "    model=\"openai/deepseek-chat\", \n",
    "    temperature=0.6, \n",
    "    messages=msg_list\n",
    ")\n",
    "    \n",
    "# Extract the content of the message from the first choice of the completion response.\n",
    "chatgpt_response = completion.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0bf00cd8-359d-485c-9f78-477c0f56f2bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\" A digital twin is a virtual representation of a physical object or system that spans its lifecycle, is updated from real-time data, and uses simulation, machine learning, and reasoning to help decision-making. It is a dynamic, digital counterpart of a physical object or process, used to understand, predict, and optimize performance in order to achieve improved business outcomes.\\n\\nDigital twins are created using various technologies including sensors, IoT, artificial intelligence, machine learning, and data analytics. They are used in various industries such as manufacturing, healthcare, automotive, aerospace, and energy to improve efficiency, reduce costs, and enhance performance.\\n\\nFor example, in manufacturing, a digital twin can simulate a production line to predict how changes will affect the line's performance. In healthcare, a digital twin of a human organ can be used to simulate medical procedures and treatments. In automotive, a digital twin of a vehicle can be used to simulate and optimize its performance and maintenance.\""
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatgpt_response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3db9481-84b8-438c-9f01-646d1cebaa25",
   "metadata": {},
   "source": [
    "## 方法2：LocalAI\n",
    "https://docs.llamaindex.ai/en/stable/examples/llm/localai/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a0ffebc8-5ed8-44c2-9fe8-841881124303",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.openai_like import OpenAILike\n",
    "# from llama_index.core.llms import LOCALAI_DEFAULTS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "73be2b0b-8ca5-4461-8221-e6531e314d98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Digital Thread 是一种在数字化生产环境中，从设计到维护的整个生命周期中，通过数字化数据链接物理世界和虚拟世界的概念。它是一个跨越多个系统、多个组织、多个时间点的连续数据流，用于跟踪产品或系统的生命周期中的所有信息和事件。\n",
      "\n",
      "Digital Thread 可以帮助企业实现更高效的设计、开发、制造和维护过程，提高质量、减少成本和加快时间到市。它还可以支持数字化生产环境中的数据分析和预测，以及对产品或系统的更好的监控和管理。\n",
      "\n",
      "Digital Thread 通常使用数字化工程（PLM）软件来实现，这些软件可以帮助企业管理产品生命周期中的所有数据，包括设计、制造、测试、维护和供应链数据。\n"
     ]
    }
   ],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "from llama_index.llms.openai_like import OpenAILike\n",
    "\n",
    "\n",
    "MAC_M1_LUNADEMO_CONSERVATIVE_TIMEOUT = 10 * 60  # sec\n",
    "\n",
    "llm = OpenAILike(\n",
    "    model=\"chat/mistral-7b/Mistral-7B-Instruct-v0.3.Q5_K_M.gguf\", \n",
    "    api_base=\"http://localhost:1234/v1\", \n",
    "    api_key=\"fake\",\n",
    "    timeout=MAC_M1_LUNADEMO_CONSERVATIVE_TIMEOUT,\n",
    ")\n",
    "\n",
    "response = llm.complete(\"什么是数字主线（Digital Thread）？\")\n",
    "print(str(response))\n",
    "\n",
    "\n",
    "# model = OpenAILike(\n",
    "#     **LOCALAI_DEFAULTS,\n",
    "    \n",
    "#     # model=\"chat/mistral-7b/Mistral-7B-Instruct-v0.3.Q5_K_M.gguf\",\n",
    "#     is_chat_model=True,\n",
    "#     timeout=MAC_M1_LUNADEMO_CONSERVATIVE_TIMEOUT,\n",
    "# )\n",
    "# response = model.chat(messages=[ChatMessage(content=\"How are you?\")])\n",
    "# print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17f8ad26-49c3-4dba-b141-e2b7bfea33a5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
